﻿using Learun.Util;
using System;
using System.Collections.Generic;
using static Learun.Application.TwoDevelopment.ZZDT_EC.ProjectSugar;
using static Learun.Util.SqlSugar.SqlSugarHelper;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-06-21 15:32
    /// 描 述：通道表
    /// </summary>
    public class ec_PanelChannelBLL : ec_PanelChannelIBLL
    {
        ec_PanelChannelService ec_PanelChannelService;
        public ec_PanelChannelBLL()
        {
            ec_PanelChannelService = new ec_PanelChannelService();
        }
        #region 获取数据
        public ec_PanelChannelEntity GetEntity(string PanelChannelId, string ProjectId)
        {
            try
            {
               var res  =  ec_PanelChannelService.GetEntity(ProjectId, PanelChannelId);
                return res;
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 获取列表数据
        /// <summary>
        /// <returns></returns>
        public IEnumerable<ec_PanelChannelEntity> GetList(string queryJson, Pagination pagination = null)
        {
            try
            {
                var queryParam = queryJson.ToJObject();
                string ProjectId = queryParam["ProjectId"]?.ToString();
                string StripID = queryParam["StripID"]?.ToString();
                string ChannelName = queryParam["ChannelName"]?.ToString();

                var tableName = TableName<ec_PanelChannelEntity>(ProjectId);
                var panelTableName = TableName<ec_PanelEntity>(ProjectId);

                var query = Db.Queryable<ec_PanelChannelEntity>().AS(tableName)
                    .WhereIF(!string.IsNullOrEmpty(StripID), x => x.StripID == StripID)
                    .WhereIF(!string.IsNullOrEmpty(ChannelName), x => x.ChannelName.Contains(ChannelName))
                    ;

                List<ec_PanelChannelEntity> list;
                if (pagination == null)
                {
                    list = query.ToList();
                }
                else
                {
                    int records = 0;
                    list = query.ToPageList(pagination.page, pagination.rows, ref records);
                    pagination.records = records;
                }
                return list;
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }
        #endregion

        #region 提交数据
        public void DeleteEntity(string PanelChannelId, string ProjectId)
        {
            try
            {
                ec_PanelChannelService.DeleteEntity(ProjectId, PanelChannelId);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }


        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public string SaveEntity(string ProjectId, string keyValue, ec_PanelChannelEntity entity)
        {
            try
            {
                ec_PanelChannelService.SaveEntity(ProjectId, keyValue, entity,out string x);
                return x;
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        #endregion

    }
}
